package edu.jsnu.operate.controller.ticket;

import beans.PageQuery;
import com.github.pagehelper.PageInfo;
import constants.FilmType;
import dto.resDto.JsonData;
import dto.resDto.RespData;
import edu.jsnu.operate.controller.ticket.common.AbstractController;
import edu.jsnu.service.ticket.film.dto.FilmDto;
import edu.jsnu.service.ticket.film.dto.FilmReceive;
import edu.jsnu.service.ticket.film.service.FilmService;
import edu.jsnu.service.ticket.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/ticket/film")
public class FilmController extends AbstractController {

    @Autowired
    private FilmService filmService;

    @RequestMapping(value = "/film.page")
    public String filmPage() {
        return "ticket/film/film";
    }

    /**
     * @param model
     * @param id
     * @param flag  新增1，编辑2，详情3
     * @return
     */
    @RequestMapping(value = "save.page")
    public String savePage(Model model,
                           @RequestParam(value = "id", required = false) Integer id,
                           @RequestParam(value = "flag", required = false, defaultValue = "1") Integer flag) {
        if (id != null) {
            FilmReceive receive = FilmReceive.Film2Receive(filmService.getFilmById(id));
            model.addAttribute("film", receive);
        }
        List<String> types = FilmType.types;
        model.addAttribute("types", types);
        model.addAttribute("flag", flag);
        return "ticket/film/save";
    }


    @RequestMapping(value = "/film.json", method = RequestMethod.GET)
    @ResponseBody
    public RespData filmData(PageQuery pageQuery) {
        try {
            List<FilmDto> filmList = filmService.getAllFilms(pageQuery, null, null);
            PageInfo<FilmDto> pageInfo = new PageInfo<FilmDto>(filmList);
            return RespData.success(pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespData.fail("查询电影数据异常，请联系管理员");
    }

    @RequestMapping(value = "/filmSelect.json", method = RequestMethod.GET)
    @ResponseBody
    public RespData filmSelectData(PageQuery pageQuery, @RequestParam(value = "keyword", required = false) String keyword) {
        try {
            List<FilmDto> filmList = filmService.getAllFilms(pageQuery, 1, keyword);
            PageInfo<FilmDto> pageInfo = new PageInfo<FilmDto>(filmList);
            return RespData.success(pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespData.fail("查询电影数据异常，请联系管理员");
    }

    @RequestMapping(value = "save.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveFilm(FilmReceive receive, HttpServletRequest request) {
        try {
            User currentUser = (User) request.getSession().getAttribute("user");
            if (receive.getId() == null) {
                return filmService.insertFilm(receive, currentUser) > 0 ? JsonData.success("新增影片成功") : JsonData.fail("新增影片失败，请稍后再试");
            } else {
                return filmService.updateFilm(receive, currentUser) > 0 ? JsonData.success("修改影片成功") : JsonData.fail("修改影片失败，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("保存影片信息异常，请联系管理员");
    }

    @RequestMapping(value = "del.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData delFilm(Integer id) {
        try {
            return filmService.deleteFilm(id) > 0 ? JsonData.success("删除影片成功") : JsonData.fail("删除影片失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("删除影片异常，请联系管理员");
    }

    @RequestMapping(value = "batchDel.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData batchDel(Integer[] ids) {
        try {
            return filmService.batchDel(ids) > 0 ? JsonData.success("批量删除影片成功") : JsonData.fail("批量删除影片失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("批量删除影片失败，请联系管理员");
    }

    @RequestMapping(value = "modifyStatus.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData modifyStatus(@RequestParam(value = "id") Integer id,
                                 @RequestParam(value = "status") Integer status) {
        try {
            return filmService.updateStatus(id, status) > 0 ? JsonData.success("修改影片状态成功") : JsonData.fail("修改影片状态失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("修改影片状态异常，请联系管理员");
    }
}

